package project.innovation.mooddiagnosis.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import project.innovation.mooddiagnosis.service.impl.ModelServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Component
public class Executor {
    private static final Logger logger = LoggerFactory.getLogger(ModelServiceImpl.class);

    @Value("${MY_DEBUG}")
    private static Boolean my_debug=Boolean.TRUE;

    public static void executeExpect(String shellPath, String userId, String mesasge) throws IOException, InterruptedException {
        String[] cmd = {"expect", shellPath, userId, mesasge};
        logger.info("expect cmd: expect {} {} {}",shellPath, userId, mesasge);
        Process process = Runtime.getRuntime().exec(cmd);

        String line;
        // 读取标准输出
        if(my_debug){
            my_debug_output(process);
        }
        // 读取错误输出
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        StringBuilder errorStr = new StringBuilder();
        while ((line = errorReader.readLine()) != null) {
            errorStr.append(line).append("\n");
        }

        errorReader.close();
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            System.err.println("Process exited with code: " + exitCode);
        }

        if (!errorStr.isEmpty()) {
            System.err.println("Error Output: " + errorStr.toString());
        }
    }

    public static void executeExpect(String shellPath, String filename) throws Exception {
        String[] cmd = {"expect", shellPath, filename};
        logger.info("expect cmd: {}","expect" + shellPath + filename);
        Process process = Runtime.getRuntime().exec(cmd);

        String line;
        // 读取标准输出
        if(my_debug){
            my_debug_output(process);
        }
        // 读取错误输出
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        StringBuilder errorStr = new StringBuilder();
        while ((line = errorReader.readLine()) != null) {
            errorStr.append(line).append("\n");
        }

        errorReader.close();
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            System.err.println("Process exited with code: " + exitCode);
        }

        if (!errorStr.isEmpty()) {
            System.err.println("Error Output: " + errorStr.toString());
        }
    }

    public static void executorShell(String shellPath, String filename) throws Exception {
        String[] cmd = {"shell", shellPath, filename};
        logger.info("shell cmd: {}","shell" + shellPath + filename);
        Process process = Runtime.getRuntime().exec(cmd);

        String line;
        // 读取标准输出
        if(my_debug){
            my_debug_output(process);
        }
        // 读取错误输出
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        StringBuilder errorStr = new StringBuilder();
        while ((line = errorReader.readLine()) != null) {
            errorStr.append(line).append("\n");
        }

        errorReader.close();
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            System.err.println("Process exited with code: " + exitCode);
        }

        if (!errorStr.isEmpty()) {
            System.err.println("Error Output: " + errorStr.toString());
        }
    }

    static void my_debug_output(Process process) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    }

}
